get list of folders in directory python

42

get list of folders in directory python -

import os 
my_list = os.listdir('My_directory')

python list directories only -

>>> [ name for name in os.listdir(thedir) if os.path.isdir(os.path.join(thedir, name)) ]
['ctypes', 'distutils', 'encodings', 'lib-tk', 'config', 'idlelib', 'xml', 'bsddb', 'hotshot', 'logging', 'doc', 'test', 'compiler', 'curses', 'site-packages', 'email', 'sqlite3', 'lib-dynload', 'wsgiref', 'plat-linux2', 'plat-mac']

python get list of files in directory -

from os import listdir
file_list = listdir(folder_path)

Comments

Submit
0 Comments